package com.anjue.utils
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;

	public class BitmapUtils
	{
		/**
		 * 绘制位图
		 * @param source
		 * @param repairRegistPoint
		 * @param inflatePoint
		 * @return 
		 */
		public static function drawBitmap(source:*, repairRegistPoint:Boolean=false,transparent:Boolean=true,fillColor:uint=0,inflatePoint:Point=null,mirroring_x:Boolean=false,mirroring_y:Boolean=false):Bitmap
		{
			var rect:Rectangle=source.getBounds(source.parent);
			if(!rect.isEmpty()){
				if(inflatePoint)rect.inflatePoint(inflatePoint);
				var offsetx:int=Math.ceil(source.x - rect.x);
				var offsety:int=Math.ceil(source.y - rect.y);
				var _matrix:Matrix=new Matrix(1, 0, 0, 1, offsetx, offsety);
				if(mirroring_x){
					offsetx = rect.width-offsetx;
					_matrix.tx=rect.width-_matrix.tx;
					_matrix.a=-1
				}
				if(mirroring_y){
					offsety = rect.height-offsety;
					_matrix.ty=rect.height-_matrix.ty;
					_matrix.d=-1
				}
				//_matrix.tx*=-1
				var bmd:BitmapData=new BitmapData(Math.ceil(rect.width+1), Math.ceil(rect.height+1),transparent, fillColor);
				bmd.draw(source, _matrix);
			}else{
				return null;
			}
			var bitmap:Bitmap=new Bitmap(bmd);
			if (repairRegistPoint)
			{
				bitmap.x=offsetx * -1;
				bitmap.y=offsety * -1;
			}
			return bitmap;
		}
		
		public static function drawBitmapData(source:*,transparent:Boolean=true,fillColor:uint=0,inflatePoint:Point=null,mirroring_x:Boolean=false,mirroring_y:Boolean=false):BitmapDataInfo
		{
			var info:BitmapDataInfo = new BitmapDataInfo();
			
			var rect:Rectangle=source.getBounds(source.parent);
			if(!rect.isEmpty()){
				if(inflatePoint)rect.inflatePoint(inflatePoint);
				var offsetx:int=Math.ceil(source.x - rect.x);
				var offsety:int=Math.ceil(source.y - rect.y);
				var _matrix:Matrix=new Matrix(1, 0, 0, 1, offsetx, offsety);
				if(mirroring_x){
					offsetx = rect.width-offsetx;
					_matrix.tx=rect.width-_matrix.tx;
					_matrix.a=-1
				}
				if(mirroring_y){
					offsety = rect.height-offsety;
					_matrix.ty=rect.height-_matrix.ty;
					_matrix.d=-1
				}
				//_matrix.tx*=-1
				var bmd:BitmapData=new BitmapData(Math.ceil(rect.width+1), Math.ceil(rect.height+1),transparent, fillColor);
				bmd.draw(source, _matrix);
				info.bitmapData = bmd;
			}else{
				return null;
			}
			
			info.offsetx=offsetx * -1;
			info.offsety=offsety * -1;
			return info;
		}
	}
}